package com.woniu.controller;

import com.woniu.entity.Result;
import com.woniu.entity.vo.UserCarVO;
import com.woniu.pojo.Car;
import com.woniu.service.CarService;
import com.woniu.utils.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Ma.Lei
 * @description
 * @date 2022/12/22 19:09
 */
@Api(tags = "用户车辆控制类")
@RestController
@RequestMapping("/car")
public class CarController extends BaseController {
    @Autowired
    private CarService carService;

    /**
     * 查询某个用户下绑定的汽车
     * @param id
     * @return
     */
    @ApiOperation("查询用户下车辆")
    @GetMapping("/listCar/{id}")
    public Result listCar(@PathVariable String id){
        List<UserCarVO> listCar = carService.getByNumber(id);
        return toDataResult(listCar);
    }
    @ApiOperation("新增车辆")
    @PostMapping("/addCar/{id}")
    public Result addCar(@RequestBody(required = false) UserCarVO userCar,@PathVariable String id){
        if (userCar.getCarNumber() == null || userCar.getCarNumber().length() == 0){
            return new Result(500,"请输入车牌号");
        }
        if (userCar.getCarType() == null || userCar.getCarType().length() == 0){
            return new Result(500,"请选择车辆类型");
        }
        List<UserCarVO> listCar = carService.getByNumber(id);
        if ( listCar.size() == 5 ){
            return new Result(500,"最多绑定5辆！");
        }
        Car car = new Car();
        car.setCarNumber(userCar.getCarNumber());
        car.setCarType(userCar.getCarType());
        car.setUserId(id);
        car.setIsDeleted("0");
        boolean flag = carService.save(car);
        if (flag){
            return new Result(200,"添加成功");
        }
        return new Result(200,"添加失败");
    }
    @ApiOperation("解绑车辆")
    @PutMapping("/deleteCar/{id}")
    public Result deleteCar(@RequestBody(required = false) UserCarVO userCar,@PathVariable String id){
        Boolean flag = carService.deleteCar(userCar.getCarNumber(),id);
        if (flag){
            return new Result(200,"解绑成功");
        }
        return new Result(200,"已经解绑或解绑失败");
    }
}
